题 目:Flash全局变量让你告别繁琐的路径
当然,关于传统方法和全局变量引用法之间是否存在效率差异,这个还说不清楚。我做过如下测试
_global.r_hint_mc = _root.createEmptyMovieClip("a", 1);
//前缀r,代表reference,指向的意思。
with (r_hint_mc) {
swapDepths(10);
}
with (a) {
trace(getDepth());
//可见with方法可以成功调用全局MC的指向,深度的确给改变了。
}
b.onRelease = function() {
testa = getTimer();
trace("called");
for (var j = 1; j<100000; j++) {
r_hint_mc.newArray = new Array()
r_hint_mc.newArray[j]=r_hint_mc._currentframe
// the result is about 2400 - 2500
// No big change in MC
// the result is about 2250 in a MCEvent!!!
//----- Local Looking
//this._parent.a.newArray = new Array();
//this._parent.a.newArray[j] = r_hint_mc._currentframe;
// the result is about 2200 - 2300
// the result is about 2300 - 2500 in a MC!!!!
// the result is about 2050 in a MCEvent
}
trace(getTimer()-testa);
};
/* 结论:
全局引用:
在同一个时间轴下,不如传统引用
在不同时间轴下(单独放到MC的时间轴),优越于传统引用
在一个MC事件处理中(并不放到这个MC的时间轴中),不如传统引用
个人觉得第三种(即事件处理)在比较复杂的应用中最频繁用到,故也体现出全局变量的一些效率劣势,但是相比而言,程序的可维护性着实提高了.
|